﻿--[[
   @brief    descprtion: log_collect_control
   @author   hanmanhuang
   @version  1.0
   @date     2017-09-24
--]]

log_collect_control = 
{
    init = function()
        log_collect_control.inner_log("init");
        ierd_tgp.tpf_ui.Require("log_collect\\model.lua");
    end;

    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[log_collect_control] %s", msg), core_msg);
    end;

    onLoginSuccess = function(event_id, json_str)
        log_collect_control.inner_log("onLoginSuccess: " .. json_str);
        if log_collect_control.checkIsLauncher() == false then
            return;
        end;

        local game_id = log_collect_control.parseLauncherGameId();
        if game_id == 0 then
            return;
        end;
        log_collect_model.game_id = game_id;
        
        if log_collect_control.checkFeatureGray() == false then
            log_collect_control.inner_log("not match feature gray");
            return;
        end;
        
        if log_collect_control.checkGameGray(log_collect_model.game_id) == false then
            log_collect_control.inner_log("not match game gray");
            return;
        end;

        log_collect_control.uploadLauncherLog();
    end;

    -- 是否启动器启动
    checkIsLauncher = function()
        local launcher_cfg = comm_center.call_service("Srv_IsLaunchedByGame",{});
        if launcher_cfg == nil or launcher_cfg.is_launcher == nil or launcher_cfg.is_launcher ~= 1 then
            log_collect_control.inner_log("not start by launcher");
            return false;
        end;
        return true;
    end;
    
    -- 获取启动的游戏id
    parseLauncherGameId = function()
        local launcher_game_id = 0;
        local launcher_info = comm_center.call_service("Srv_GetLauncherInfo",{});
        if launcher_info == nil or launcher_info.info == nil then
            log_collect_control.inner_log("get launcher info empty");
            return 0;
        end;
        local info_table = base_utility.json_to_table(launcher_info.info);
        if info_table == nil or info_table.game_id == nil then
            log_collect_control.inner_log("parse launcher info error");
            return 0;
        end;
        launcher_game_id = tonumber(info_table.game_id);
        if launcher_game_id == nil or launcher_game_id == 0 then
            log_collect_control.inner_log("get launcher info game_id empty");
            return 0;
        end;
        return launcher_game_id;
    end;

    checkFeatureGray = function()
        local gray = false;
        local feature_mgr = base_utility.get_component_adapter("IFeature_mgr",
                                "ierd_tgp.feature.GetFeatureMgrAdapter();");
        if feature_mgr ~= nil then
            gray = feature_mgr:support_feature(ierd_tgp.feature.launch_log_collect);
        end
        return gray;
    end;

    checkGameGray = function(game_id)
        local gray = false;
        local data = {key = "launcher_start_log_collect"};
        local cfg = comm_center.call_service("i_svr_get_common_cfg", data);
        if nil == cfg then
            return false;
        end
        
        if cfg.value == nil or cfg.value == "" then
            return false;
        end

        local cfg_tb = base_utility.json_to_table(cfg.value);
        if cfg_tb == nil then
            return false;
        end

        for k, v in pairs(cfg_tb) do
            if v ~= nil and v.game_id ~= nil and v.game_id ~= "" and
               v.game_id ~= "0" and v.game_id == tostring(game_id) then
                log_collect_control.inner_log("match game id gray: "..tostring(game_id));
                gray = true;
                break;
            end
        end
        return gray;
    end;

    uploadLauncherLog = function()
        local upload_info = {};
        local json_info = {};
        json_info.moduleid = 52000001;
        json_info.modulename = LoadStr("LOG_COLLECT_LAUNCHER_LOG");
        json_info.message = "(gameid:"..log_collect_model.game_id..")";
        upload_info.json = base_utility.table_to_json(json_info);
        upload_info.log_paths = {};
        table.insert(upload_info.log_paths, "%tgp_root%\\tcls\\log\\*");
        table.insert(upload_info.log_paths, "%tgp_root%\\tcls\\TLog\\*");
        comm_center.call_service("i_service_feedback_upload_form", upload_info);
    end;
};

log_collect_control.init();